const { isRequired } = require("../utils");
const { Sequelize } = require("sequelize");

module.exports = (sequelize, DataTypes) => {
  const MenuButton = sequelize.define(
    "MenuButton",
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: Sequelize.UUIDV4,
        allowNull: false,
        primaryKey: true,
        comment: "按钮id",
      },
      parentId: {
        type: DataTypes.UUID,
        comment: "父级id,只允许菜单id",
        allowNull: false,
        validate: isRequired("菜单Id"),
      },
      perms: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: "权限标识",
        // 设置唯一不能重复
        unique: {
          msg: "权限标识已存在",
        },
        validate: isRequired("按钮权限标识"),
      },
      name: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: "按钮名称",
        validate: isRequired("按钮名称"),
      },
      sort: {
        type: DataTypes.INTEGER,
        comment: "排序",
        defaultValue: 0,
      },
      isUse: {
        type: DataTypes.INTEGER,
        comment: "是否使用",
        defaultValue: 1,
        validate: {
          isIn: {
            args: [[0, 1]],
            msg: "isUse只能填0和1",
          },
        },
      },
    },
    {
      timestamps: true,
      tableName: "system_buttons",
      createdAt: "createTime",
      updatedAt: "updateTime",
      comment: "按钮权限表",
    }
  );

  return MenuButton;
};
